#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include<queue>
using namespace std;
#define MAXL 1000
vector<int>a[MAXL];
bool vis[MAXL];
void dfs(int u)
{
	for (int i = 0; i < a[u].size(); i++)
	{
		if (vis[a[u][i]])continue;
		vis[a[u][i]] = 1;
		dfs(a[u][i]);
	}
}
int main()
{
	int n, m;
	int u, v,k=0;
	cin >> n;
	while (n)
	{
		cin >> m;
		while (m--)
		{
			cin >> u >> v;
			a[u].push_back(v);
			a[v].push_back(u);
		}
		for (int i = 1; i <= n; i++)
		{
			if (vis[i])continue;
			dfs(i);
			k++;
		}
		cout << k-1 << endl;
		k = 0;
		for (int i = 1; i <= n; i++)
		{
			vis[i] = 0;
			a[i].clear();
		}
		cin >> n;
	}
	return 0;
}